#include <iterator>
#include "MarkupSTL.h"
#include "global.h"
#include "cmd.h"
#include "Game.h"
#include "RedisClient.h"
#include "Logic.h"
#include "json.h"
#include "globalExtern.h"
#include "Host.h"
#include "NodeServer.h"

CGameServer::CGameServer()
{
}

CGameServer::~CGameServer()
{
}

bool CGameServer::InitGame()
{
	return true;
}

int CGameServer::OnBYTimeOut(int Timerid)
{
	return 0;
}

int CGameServer::ProcessPacket(SocketHandler * pHandler, NETInputPacket* inputPkg)
{
	int cmd = inputPkg->GetCmdType();
	switch (cmd)
	{
	case DISPATCH_SRV_P2P:
		this->ProP2P(pHandler, inputPkg);
		break;
	case DISPATCH_SRV_BROASCAST:
		this->ProBroadcast(pHandler, inputPkg);
		break;
	case DISPATCH_SRV_PUSH_CMD:
		this->ProPushCmd(pHandler, inputPkg);
		break;
	case DISPATCH_SRV_REGISTER_CMD:
		this->ProRegisterCmd(pHandler, inputPkg);
		break;
	case DISPATCH_SRV_CANCEL_CMD:
		this->ProCancelCmd(pHandler, inputPkg);
		break;
	default:
		log_error("cmd=0x%x,  svid=%d, err no proc", cmd, pHandler->getId());
		break;
	}
	return 0;
}

int CGameServer::ProP2P(SocketHandler * pHandler, NETInputPacket* inputPkg)
{
	int src_type = inputPkg->ReadShort();
	int src_svid = inputPkg->ReadInt();
	int dst_type = inputPkg->ReadShort();
	int dst_svid = inputPkg->ReadInt();
	char pkg_buf[10240] = { 0 };
	int len = inputPkg->ReadBinary(pkg_buf, 10240);
	NETOutputPacket outPkg;
	outPkg.Copy(pkg_buf, len);
	this->m_pHost->SendPacket(&outPkg, dst_type, dst_svid);
	return 0;
}

int CGameServer::ProBroadcast(SocketHandler * pHandler, NETInputPacket* inputPkg)
{
	int src_type = inputPkg->ReadShort();
	int src_svid = inputPkg->ReadInt();
	int dst_type = inputPkg->ReadShort();
	int dst_svid = inputPkg->ReadInt();
	char pkg_buf[10240] = { 0 };
	int len = inputPkg->ReadBinary(pkg_buf, 10240);
	NETOutputPacket outPkg;
	outPkg.Copy(pkg_buf, len);


	SrvDataList& serverList = m_pHost->m_pNode->m_svrTypeList[dst_type];
	SrvDataList::iterator iter = serverList.begin();
	for (; iter != serverList.end(); iter++)
	{
		CClientHandler* pClient = iter->second->pHandler;
		if (NULL != pHandler)
			pClient->Send(&outPkg);
	}
	return 0;
}



int CGameServer::ProPushCmd(SocketHandler * pHandler, NETInputPacket* inputPkg)
{
	int src_type = inputPkg->ReadShort();
	int src_svid = inputPkg->ReadInt();
	int cmd = inputPkg->ReadInt();

	char pkg_buf[10240] = { 0 };
	int len = inputPkg->ReadBinary(pkg_buf, 10240);
	NETOutputPacket outPkg;
	outPkg.Copy(pkg_buf, len);

	SvidVect delVect;
	SvidSet& svidSet = m_cmdSubMap[cmd];
	SvidSet::iterator iter = svidSet.begin();
	for (; iter != svidSet.end(); iter++)
	{
		int64 type_svid = *iter;
		int dst_type = (int)((type_svid >> 32) & 0xFFFFFFFFLL);
		int dst_svid = (int)(type_svid & 0xFFFFFFFFLL);

		SrvDataList& serverList = m_pHost->m_pNode->m_svrTypeList[dst_type];
		SrvDataList::iterator iter_dst = serverList.find(dst_svid);
		if (iter_dst != serverList.end())
		{
			CClientHandler* pClient = iter_dst->second->pHandler;
			if (NULL != pHandler)
				pClient->Send(&outPkg);
		}
		else
		{// err svid
			delVect.push_back(type_svid);
		}
	}

	if (delVect.size() > 0)
	{
		for (int i = 0; i < delVect.size(); i++)
		{
			svidSet.erase(delVect[i]);
		}
	}


	return 0;
}

int CGameServer::ProRegisterCmd(SocketHandler * pHandler, NETInputPacket* inputPkg)
{
	int cmd = inputPkg->ReadInt();
	int64 svid = pHandler->getId();
	int64 type = pHandler->getType();
	int64 type_svid = (type << 32 | svid);

	SvidSet& svidSet = m_cmdSubMap[cmd];
	svidSet.insert(type_svid);

	return 0;
}

int CGameServer::ProCancelCmd(SocketHandler * pHandler, NETInputPacket* inputPkg)
{
	int cmd = inputPkg->ReadInt();
	int64 svid = pHandler->getId();
	int64 type = pHandler->getType();
	int64 type_svid = (type << 32 | svid);

	SvidSet& svidSet = m_cmdSubMap[cmd];
	svidSet.erase(type_svid);

	return 0;
}





